using HCore.IOC.Beans.Factory.Attribute.Attr;
using HCore.Tests.Runtime.DianDianDian.Command;
using HCore.Tests.Runtime.DianDianDian.Model;
using HCore.Unity.Behaviour;

namespace HCore.Tests.Runtime.DianDianDian.UI
{
    public class GameStartPanel : HCoreMonoBehaviour
    {
        [Autowired] private IGameModel mGameModel;

        private void Start()
        {
            Get("Btn-开始").AddButtonClickListener(ActionGameStart);
            Get("Btn-购买生命").AddButtonClickListener(ActionBuyLife);

            mGameModel.RegisterGoldChangedListener(ActionGameModelGoldChanged);
            mGameModel.RegisterLifeChangedListener(ActionGameModelLifeChanged);
            mGameModel.RegisterBestScoreChangedListener(ActionGameModelBestScoreChanged);

            ActionGameModelBestScoreChanged(mGameModel.BestScore, 0);
        }


        protected override void OnDestroy()
        {
            mGameModel.UnRegisterGoldChangedListener(ActionGameModelGoldChanged);
            mGameModel.UnRegisterLifeChangedListener(ActionGameModelLifeChanged);
            mGameModel.UnRegisterBestScoreChangedListener(ActionGameModelBestScoreChanged);
            base.OnDestroy();
        }

        private void ActionBuyLife()
        {
            // 发送购买生命命令
            PublishEvent(new BuyLifeCommand());
        }

        private void ActionGameStart()
        {
            Get().SetActive(false);
            // 执行游戏启动命令
            PublishEvent(new StartGameCommand());
        }

        private void ActionGameModelLifeChanged(int newValue, int oldValue)
        {
            Get("Text-生命").Text.text = $"{newValue}";
        }

        private void ActionGameModelBestScoreChanged(int newValue, int oldValue)
        {
            Get("Text-最高分").Text.text = $"{newValue}";
        }

        private void ActionGameModelGoldChanged(int newValue, int oldValue)
        {
            Get("Btn-购买生命").SetActive(newValue > 0);
            Get("Text-金币").Text.text = $"{newValue}";
        }
    }
}